package ui;

import java.awt.EventQueue;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.text.MaskFormatter;

import java.awt.Font;
import java.awt.event.KeyEvent;
import java.text.ParseException;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;

public abstract class AbstractTodoWindow extends JFrame {
	
	protected AbstractTodoPresenter presenter;
	protected JTextField titleField;
	protected JTextField startField;
	protected JTextField endField;
	protected JTextArea descriptionArea;
	protected JComboBox<String> categoryBox;
	private JButton btnNewButton;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AbstractTodoWindow window = new AddTodoWindow(new MainWindow());
					window.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public AbstractTodoWindow(MainWindow mw) {
		presenter = createPresenter();
		initialize();
		setLocationRelativeTo(mw);
	}
	
	abstract protected AbstractTodoPresenter createPresenter();
	
	abstract protected String myTitle();

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		setBounds(100, 100, 300, 359);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		getContentPane().setLayout(null);

		titleField = new JTextField();
		titleField.setDocument(new JTextFieldLimit(20));
		titleField.setBounds(99, 85, 134, 20);
		getContentPane().add(titleField);
		titleField.setColumns(20);

		categoryBox = new JComboBox<String>();
		categoryBox.setBounds(99, 110, 134, 20);
		getContentPane().add(categoryBox);

		MaskFormatter mf = null;
		try {
			mf = new MaskFormatter("####/##/## ##:##");
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		startField = new JFormattedTextField(mf);
		startField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				presenter.startDateEntered();
			}
		});
		startField.setBounds(99, 205, 134, 20);
		getContentPane().add(startField);
		startField.setColumns(10);

		endField = new JFormattedTextField(mf);
		endField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				presenter.endDateEntered();
			}
		});
		endField.setBounds(99, 231, 134, 20);
		getContentPane().add(endField);
		endField.setColumns(10);

		JButton saveButton = new JButton("Save");
		saveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				presenter.saveButtonClicked();
			}
		});
		saveButton.setBounds(160, 260, 73, 23);
		getContentPane().add(saveButton);

		JLabel lblTitle = new JLabel("Title:");
		lblTitle.setBounds(65, 85, 30, 14);
		getContentPane().add(lblTitle);

		JLabel lblCategory = new JLabel("Category:");
		lblCategory.setBounds(38, 110, 55, 14);
		getContentPane().add(lblCategory);

		JLabel lblDescription = new JLabel("Description:");
		lblDescription.setBounds(24, 132, 79, 14);
		getContentPane().add(lblDescription);

		JLabel lblWindowTitle = new JLabel(myTitle());
		lblWindowTitle.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblWindowTitle.setBounds(24, 21, 157, 14);
		getContentPane().add(lblWindowTitle);

		JLabel lblBegins = new JLabel("Begins:");
		lblBegins.setBounds(49, 208, 59, 14);
		getContentPane().add(lblBegins);

		JLabel lblEnds = new JLabel("Ends:");
		lblEnds.setBounds(59, 234, 54, 14);
		getContentPane().add(lblEnds);

		descriptionArea = new JTextArea(6, 20);
		descriptionArea.setLineWrap(true);
		descriptionArea.setBounds(99, 137, 134, 63);
		getContentPane().add(descriptionArea);

	}

	public AbstractTodoPresenter getPresenter() {
		return presenter;
	}

	public void addToComboBox(String categoryName) {
		categoryBox.addItem(categoryName);
	}
	
	public JTextField getStartField() {
		return startField;
	}

	public JTextField getEndField() {
		return endField;
	}
	
	public JTextField getTitleField() {
		return titleField;
	}
	
	public String getTodoName(){
		String todoName = titleField.getText();
		return todoName;
	}

	public String getTodoDesctiption() {
		String todoDescription = descriptionArea.getText();
		return todoDescription;
	}

	public String getTodoStartTime() {
		String todoStartTime = startField.getText();
		return todoStartTime;
	}

	public String getTodoEndTime() {
		String todoEndTime = endField.getText();
		return todoEndTime;
	}

	public String getTodoCategory() {
		String todoCategory =  (String) categoryBox.getSelectedItem();
		return todoCategory;
	}
}
